import Vue from "vue";
import Vuex from "vuex";



//引入权限菜单
import * as api from "@/api/permission"

//引入递归路由
// import recursionRoutes from "@/router/recursionRoutes"

//引入项目所有的路由
import allRoutes from "@/router/generateRoutes"

//把recursion函数递归创建用户路由的方法引入
import recursionRoutes from "@/router/recursionRoutes"
//引入路由
import router from "@/utils/routerGuard"
let routes = allRoutes.find(route => route.name === "home")

//引入动态路由
import dynamicRoutes from '@/router/dynamicRoutes' 

Vue.use(Vuex);

export default {
  namespaced: true,
  state: {
    userMenu:[],
    //面包屑
    crumb:[]
  },
  getters: {},
  mutations: {
    //设置用户菜单
    SET_USER_MENU(state,payload){
      state.userMenu = [...payload]//浅拷贝

      //动态添加路由，使用router.addRoute这个方法
      dynamicRoutes.children = [...dynamicRoutes.children,...payload]
      router.addRoute(dynamicRoutes)
    },
    SET_CRUMB(state,payload){
       //设置面包屑
      state.crumb = payload
    }
  },
  actions: {
    //获取远程菜单
    FETCH_MENU_LIST({commit}){
      return api.getMenuListApi()
      .then(res => {
        // console.log(res.data.menuList);
        //后端返回的菜单数据
        // let { menuList } = res.data;
        let arr = recursionRoutes(res.data.menuList,routes.children)
        // console.log(userMenu);
        //让mutation进行处理
        commit('SET_USER_MENU',arr)
      })
    }
  },
  modules: {},
};
